/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#ifndef CORE_REGIONS_ENDPOINT_H_
#define CORE_REGIONS_ENDPOINT_H_

#include <string.h>

#include <set>
#include <vector>
#include <string>

#include "ProductDomain.h"

namespace aliyun {

namespace regions {

class Endpoint {
 public:
  typedef std::string string;

  Endpoint(string name, std::set<string> regions,
           std::vector<ProductDomain>& products)
      : name_(name),
        regionIds_(regions),
        productDomains_(products) {
  }

  const string& getName() const {
    return name_;
  }

  void setName(string name) {
    name_ = name;
  }

  const std::set<string>& getRegionIds() const {
    return regionIds_;
  }

  void setRegionIds(const std::set<string>& regionIds) {
    regionIds_ = regionIds;
  }

  const std::vector<ProductDomain>& getProductDomains() const {
    return productDomains_;
  }

  void setProductDomains(const std::vector<ProductDomain>& productDomains) {
    productDomains_ = productDomains;
  }

  static const ProductDomain* findProductDomain(
      const std::vector<Endpoint>& endpoints, string regionId, string product) {
    if (regionId.length() == 0 || product.length() == 0
        || endpoints.size() == 0) {
      return NULL;
    }

    for (unsigned i = 0; i < endpoints.size(); i++) {
      const std::set<string>& regions = endpoints[i].getRegionIds();
      if (regions.find(regionId) != regions.end()) {
        return findProductDomainByProduct(endpoints[i].getProductDomains(),
                                          product);
      }
    }

    return NULL;
  }

  static const ProductDomain* findProductDomainByProduct(
      const std::vector<ProductDomain>& productDomains, string product) {
    if (productDomains.size() == 0) {
      return NULL;
    }

    for (unsigned i = 0; i < productDomains.size(); i++) {
      if (::strncasecmp(product.c_str(),
                        productDomains[i].getProductName().c_str(),
                        product.length()) == 0) {
        return &productDomains[i];
      }
    }

    return NULL;
  }

 private:
  string name_;
  std::set<string> regionIds_;
  std::vector<ProductDomain> productDomains_;
};

}  // namespace regions

}  // namespace aliyun

#endif /* CORE_REGIONS_ENDPOINT_H_ */
